#pragma once
#include <iostream>
#include <string>
#include <mysql/mysql.h>
#include "../comm/httplib.h"
#include <jsoncpp/json/json.h>

#include "head_mysql.hpp"
#include "timestamp.hpp"
#include "generateSessionId.hpp"
#include "generateBID.hpp"
#include "../comm/log.hpp"

using namespace httplib;
using namespace std;
using namespace ns_log;

void handle_verify_fp(const Request &req, Response &res)
{
    loadCurrentBID();                                         // 启动时加载 currentBID
    time_t TIME_STAMP_END = time(nullptr);                    // 获取当前时间戳
    size_t valid_time = TIME_STAMP_END - *TIME_STAMP_BEGIN_1; // 计算时间差

    // 解析 JSON 请求体
    Json::Value json_data;
    Json::CharReaderBuilder reader;
    std::istringstream iss(req.body);
    std::string errs;

    if (!Json::parseFromStream(reader, iss, &json_data, &errs))
    {
        res.set_content("无效 JSON", "text/plain");
        res.status = 400;
        return;
    }

    std::string username = json_data["username"].asString();
    std::string password = json_data["password"].asString();
    std::string verificationCode = json_data["verificationCode"].asString();

    MYSQL *conn = connect_db();
    if (conn == nullptr)
    {
        res.set_content("数据库连接失败", "text/plain");
        res.status = 500;
        return;
    }

    MYSQL_STMT *stmt;
    MYSQL_BIND bind[1];
    MYSQL_BIND result_bind[1];
    bool is_verified;

    // 查询验证码是否过期
    if (valid_time > VALID_TIME)
    {
        res.set_content("验证码失效，请重新发送验证码[1]", "text/plain");
        res.status = 200;
        return;
    }

    // 验证码验证逻辑 (未过期的情况)
    stmt = mysql_stmt_init(conn);
    if (!stmt)
    {
        std::cerr << "mysql_stmt_init() failed\n";
        res.set_content("验证失败", "text/plain");
        res.status = 500;
        mysql_close(conn);
        return;
    }

    // 更新用户验证状态
    stmt = mysql_stmt_init(conn);
    if (!stmt)
    {
        LOG(ERROR)<< "mysql_stmt_init() failed\n";
        res.set_content("验证失败", "text/plain");
        res.status = 500;
        mysql_close(conn);
        return;
    }

    const char *update_query = "UPDATE users SET password=? WHERE verification_token = ?";
    if (mysql_stmt_prepare(stmt, update_query, strlen(update_query)))
    {
        LOG(ERROR)<< "mysql_stmt_prepare() failed\n";
        res.set_content("验证失败", "text/plain");
        res.status = 500;
        mysql_stmt_close(stmt);
        mysql_close(conn);
        return;
    }

    MYSQL_BIND update_bind[2];
    memset(update_bind, 0, sizeof(update_bind));

    update_bind[0].buffer_type = MYSQL_TYPE_STRING;
    update_bind[0].buffer = (char *)password.c_str();
    update_bind[0].buffer_length = password.size();

    update_bind[1].buffer_type = MYSQL_TYPE_STRING;
    update_bind[1].buffer = (char *)verificationCode.c_str();
    update_bind[1].buffer_length = verificationCode.size();

    mysql_stmt_bind_param(stmt, update_bind);

    if (mysql_stmt_execute(stmt))
    {
        LOG(ERROR)<< "mysql_stmt_execute() failed\n";
        res.set_content("验证失败", "text/plain");
        res.status = 500;
    }
    else
    {
        res.status = 200;
        res.set_content("您的帐户已成功验证，密码已更新", "text/plain");
    }

    mysql_stmt_close(stmt);

    mysql_close(conn);
}
